(function(factory) {
	if ( typeof define === "function" && define.amd ) {

		// AMD. Register as an anonymous module.
		define(factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	/**
	 * @author 李球 <service@hteos.com>
	 * @class HteOS.menu.Menu 右键菜单组件
	 */
	var Menu = HteOS.menu.Menu = function(options) {
		this.selector = options.selector;
		this.items = options.items;
		this.cls = options.cls;
		this.handler = options.handler || {};
		this.events = options.events || {};
		this.id = "hte-menu-" + new Date().getTime();
		var html = this.render(this.items);
		var el = $(html);
		el.attr("id",this.id);
		el.addClass(this.cls);
		el.addClass("hte-menu");
		this.el = el;
		$(document.body).append(el);
		this.bindHanlder();
		HteOS.menu.MenuManager.register(this);
	};

	/**
	 * @prototype
	 * 所有的事件集合，支持的事件:show,hide
	 */
	Menu.prototype.events = {};
	
	Menu.prototype.handler = {};

	Menu.prototype.render = function(items,submenu) {
		var len = items.length,
			itemsHTML = "";
		var itemTpl1 = "<li class=\"hte-menu-item\" role=\"presentation\">" +
				"<a role=\"menuitem\" data-item=\"{id}\" data-group=\"{group}\" href=\"#\"> " +
					"<span class=\"hte-menu-icon {icon}\"></span>&nbsp;&nbsp;{name}" +
				"</a>" +
			"</li>";
		var itemTpl3 = "<li class=\"hte-menu-item hte-menu-dir\" role=\"presentation\" >" +
				"<span class=\"hte-menu-dir-arrow glyphicon glyphicon-menu-right\"></span>" +
				"<a role=\"menuitem\" data-item=\"{id}\" href=\"#\"> " +
					"<span class=\"hte-menu-icon {icon}\"></span>&nbsp;&nbsp;{name}" +
				"</a>" +
				"{submenu}" +
			"</li>";
		var itemTpl2 = "<li class=\"hte-menu-item\" role=\"presentation\">" +
				"{html}" +
			"</li>";
		for (var i = 0; i < len; i++) {
			var item = items[i];
			if (item != "-") {
				var html,hasSub = false;
				if(item.items && item.items.length > 0){
					html = itemTpl3;
					hasSub = true;
				}else if(!item.html){
					html = itemTpl1;
				}else{
					html = itemTpl2;
				}
				
				if(!item.icon){
					item.icon = 'glyphicon';
				}
				for (var a in item) {
					if(a != "items"){
						//自动替换属性
						html = html.replace("{" + a + "}", item[a]);
					}
				}
				
				if(hasSub){
					var submenuHtml = this.render(item.items,true);
					html = html.replace("{submenu}", submenuHtml);
				}
				
				//配置事件处理
				if(item.handler){
					this.handler[item.id] = item.handler;
				}
				
				itemsHTML += html;
			}else{
				//添加分隔符
				itemsHTML +="<li role=\"presentation\" class=\"divider\"></li>"
			}
		}
		return "<ul class=\"dropdown-menu " + (submenu ? "hte-menu-sub" : "") +"\" role=\"menu\">" + itemsHTML + "</ul>";
	};

	Menu.prototype.bindHanlder = function() {
		var me = this,el = me.getEl();
		el.on("click", "[data-item]", function(event) {
			var item = $(event.target);
			if (item.hasClass("disabled") || item.hasClass("hte-menu-dir")) {
				return;
			}
			if (!item.data("item")) {
				item = item.parents("[data-item]");
			}
			var handler = me.handler[item.data("item")];
			if (handler) {
				//try{
					handler.call(me, item.data("item"));
				/*}catch(e){
					HteOS.Console.error("执行菜单处理函数发生错误" + e);
				}*/
			}
			me.hide();
			event.stopPropagation();
		});
	};
	
	Menu.prototype.getSubMenu = function(item){
		this.getEl().find("[data-item='"+ id +"']").parent().find("hte-menu");
	};
	
	Menu.prototype.addMenuItem = function(item){
		this.getEl().find("[data-item='"+ id +"']").parent().find("hte-menu");
	};
	
	Menu.prototype.removeMenuItem = function(item){
		this.getEl().find("[data-item='"+ id +"']").parent().find("hte-menu");
	};
	
	Menu.prototype.hideMenuItem = function(item){
		this.getEl().find("[data-item='"+ id +"']").parent().find("hte-menu");
	};
	
	Menu.prototype.showMenuItem = function(item){
		this.getEl().find("[data-item='"+ id +"']").parent().find("hte-menu");
	};
	
	Menu.prototype.selectMenuItem = function(item){
		this.getEl().find("[data-item='"+ id +"']").parent().find("hte-menu");
	};

	/**
	 * 菜单显示
	 * */
	Menu.prototype.show = function(event) {
		this.target = event.target;
		this.getEl().show();
		if (this.events['show']) {
			/**
			 * @event {HteOS.menu.Menu} 显示事件
			 * */
			this.events['show'].call(this, event.target);
		}
		HteOS.menu.MenuManager.updateActive(this);
	};

	/**
	 * 隐藏菜单
	 * */
	Menu.prototype.hide = function() {
		this.getEl().hide();
		if (this.events['hide']) {
			/**
			 * @event {HteOS.menu.Menu} 隐藏时间
			 * */
			this.events['hide'].call(this);
		}
		this.target = null;
		HteOS.menu.MenuManager.clearActive(this);
	};
	
	Menu.prototype.getEl = function(){
		return this.el;
	};
	
	Menu.prototype.group = function(group){
		return this.getEl().find("[data-group='"+ group +"']");
	};
	
	Menu.prototype.get = function(id){
		return this.getEl().find("[data-item='"+ id +"']");
	};
	
	Menu.prototype.setIcon = function(id,icon){
		this.get(id).find(".glyphicon").addClass(icon);
	}
	
	Menu.prototype.disable = function(id){
		this.getEl().find("[data-item='"+ id +"']").addClass("disabled");
	};
	
	Menu.prototype.enable = function(id){
		this.getEl().find("[data-item='"+ id +"']").removeClass("disabled");
	}
}));